﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pGrid
{
  [Serializable]
  public partial class Player
  {
    // идентификатор клиента
    private int m_id;

    // очередь хода
    private int m_rating;

    // количество денег
    private int m_money;

    // имя и цвет игрока
    private string m_name;
    private Colors m_color;

    // число подключенных городов, станций, количество ресурсов
    private int m_cities_num;
    private int m_stations_num;
    private int m_coal_num;
    private int m_oil_num;
    private int m_waste_num;
    private int m_uranium_num;
    private int m_wind_num;

    // может ли игрок покупать различные типы ресурсов
    private bool can_buy_coal;
    private bool can_buy_oil;
    private bool can_buy_waste;
    private bool can_buy_uranium;
    private bool can_bet;
    private bool started_auction;

    // может ли игрок покупать станцию в этот ход
    private bool can_buy_station;

    // может ли игрок купить 4ю станцию (только если всего игроков 2 или 3)
    private bool can_buy_fourth_station;


    // станции, принадлежащие игроку (лежат не в коллекции, чтобы не было проблем с сохранением игры)
    private PowerStation m_station1;
    private PowerStation m_station2;
    private PowerStation m_station3;
    private PowerStation m_station4;

    // конструктор с параметрами
    public Player(Colors color, int rating, string name)
    {
      this.m_name = name;
      this.m_rating = rating;
      this.m_color = color;
      this.m_money = 50;
      this.m_cities_num = 0;
      this.m_stations_num = 0;
      this.m_coal_num = 0;
      this.m_oil_num = 0;
      this.m_waste_num = 0;
      this.m_uranium_num = 0;
      this.m_wind_num = 1000000;
      this.m_id = this.m_rating + 1;
      this.can_buy_coal = false;
      this.can_buy_oil = false;
      this.can_buy_waste = false;
      this.can_buy_uranium = false;
      this.can_buy_station = true;
      this.can_buy_fourth_station = false;
    }

    public bool StartedAuction
    {
      get
      {
        return started_auction;
      }
      set
      {
        started_auction = value;
      }
    }
    public bool CanBet
    {
      get
      {
        return this.can_bet;
      }
      set
      {
        this.can_bet = value;
      }
    }

    public Player()
    {
    }

    public bool BuyCoal
    {
      get
      {
        return this.can_buy_coal;
      }
      set
      {
        this.can_buy_coal = value;
      }
    }
    
    public bool BuyOil
    {
      get
      {
        return this.can_buy_oil;
      }
      set
      {
        this.can_buy_oil = value;
      }
    }

    public bool BuyWaste
    {
      get
      {
        return this.can_buy_waste;
      }
      set 
      {
        this.can_buy_waste = value;
      }
    }

    public bool BuyUranium
    {
      get
      {
        return this.can_buy_uranium;
      }
      set
      {
        this.can_buy_uranium = value;
      }
    }

    public bool BuyStation
    {
      get
      {
        return this.can_buy_station;
      }
      set
      {
        this.can_buy_station = value;
      }
    }

    public bool BuyFourthStation
    {
      get
      {
        return this.can_buy_fourth_station;
      }
      set
      {
        this.can_buy_fourth_station = value;
      }
    }
    public int CitiesNum
    {
      get
      {
        return m_cities_num;
      }
      set
      {
        this.m_cities_num = value;
      }
    }
    
    public int CoalNum
    {
      get
      {
        return this.m_coal_num;
      }
      set 
      {
        this.m_coal_num = value;
        if(!CheckBuyResourses(ResourseType.coal))
          this.BuyCoal = false;
        if (CheckBuyResourses(ResourseType.mixed))
          this.BuyCoal = true;
      }
    }
    
    public Colors Color
    {
      get
      {
        return this.m_color;
      }
    }

    public int Id
    {
      get
      {
        return this.m_id;
      }
      set
      {
        this.m_id = value;
      }
    }
    
    public int Money
    {
      get
      {
        return this.m_money;
      }
      set
      {
        this.m_money = value;
      }
    }

    public string Name
    {
      get
      {
        return this.m_name;
      }
    }

    public int OilNum
    {
      get
      {
        return m_oil_num;
      }
      set
      {
        this.m_oil_num = value;
        if(!CheckBuyResourses(ResourseType.oil))
          this.BuyOil = false;
        if (CheckBuyResourses(ResourseType.mixed))
          this.BuyOil = true;
      }
    }

    public int Rating
    {
      get
      {
        return this.m_rating;
      }
      set
      {
        this.m_rating = value;
      }
    }

    public PowerStation Station1
    {
      get
      {
        return this.m_station1;
      }
      set
      {
        this.m_station1 = value;
        if (value != null)
        {
          ChangeBuyResourses(value.Type);
          this.m_stations_num++;
        }
      }
    }

    public PowerStation Station2
    {
      get
      {
        return this.m_station2;
      }
      set
      {
        this.m_station2 = value;
        if (value != null)
        {
          ChangeBuyResourses(value.Type);
          this.m_stations_num++;
        }
      }
    }

    public PowerStation Station3
    {
      get
      {
        return this.m_station3;
      }
      set
      {
        this.m_station3 = value;
        if (value != null)
        {
          ChangeBuyResourses(value.Type);
          this.m_stations_num++;
        }
      }
    }

    public PowerStation Station4
    {
      get
      {
        return this.m_station4;
      }
      set
      {
        this.m_station4 = value;
        if (value != null)
        {
          ChangeBuyResourses(value.Type);
          this.m_stations_num++;
        }
      }
    }

    public int StationsNum
    {
      get
      {
        return this.m_stations_num;
      }
      set 
      {
        this.m_stations_num = value;
      }
    }

    public int UraniumNum
    {
      get
      {
        return this.m_uranium_num;
      }
      set
      {
        this.m_uranium_num = value;
        if(!CheckBuyResourses(ResourseType.uranium))
          this.BuyUranium = false;
      }
    }

    public int WasteNum
    {
      get
      {
        return this.m_waste_num;
      }
      set
      {
        this.m_waste_num = value;
        if(!CheckBuyResourses(ResourseType.waste))
          this.BuyWaste = false;
      }
    }

    public int WindNum
    {
      get
      {
        return this.m_wind_num;
      }
    }
   
    public override string ToString()
    {
      string temp = "";
      temp += this.m_name + " " + this.m_rating;
      return temp;
    }

    private void ChangeBuyResourses(ResourseType type)
    {
      if (type == ResourseType.coal)
        this.BuyCoal = true;
      if (type == ResourseType.oil)
        this.BuyOil = true;
      if (type == ResourseType.waste)
        this.BuyWaste = true;
      if (type == ResourseType.uranium)
        this.BuyUranium = true;
      if (type == ResourseType.mixed)
      {
        this.BuyOil = true;
        this.BuyCoal = true;
      }
        
    }
    private bool CheckBuyResourses(ResourseType type)
    {
      int needed_num = 0, real_num = 0;
      if (Station1 != null)
        if (Station1.Type == type)
        {
          needed_num += 2 * Station1.NeededResNum;
          real_num += Station1.RealResNum;
        }
      if (Station2 != null)
        if (Station2.Type == type)
        {
          needed_num += 2 * Station2.NeededResNum;
          real_num += Station2.RealResNum;
        }
      if (Station3 != null)
        if (Station3.Type == type)
        {
          needed_num += 2 * Station3.NeededResNum;
          real_num += Station3.RealResNum;
        }
      if (Station4 != null)
        if (Station4.Type == type)
        {
          needed_num += 2 * Station4.NeededResNum;
          real_num += Station4.RealResNum;
        }
      if(real_num < needed_num)
        return true;
      else
        return false;
    }

    public bool TryPutResourseOnStation(PowerStation st, ResourseType type)
    {
      if (st == null) return false;
      if (st.RealResNum < st.NeededResNum * 2)
      {
        if ((type == ResourseType.coal) || (type == ResourseType.oil))
        {
          if ((st.Type == type) || (st.Type == ResourseType.mixed))
            st.RealResNum++;
          else
            return false;
        }
        else
        {
          if (st.Type == type)
            st.RealResNum++;
          else
            return false;
        }
        return true;
      }
      else return false;
    }

    public bool TryBuyStation(PowerStation newSt)
    {
      if (this.Station1 == null)
      {
        Station1 = newSt;
        return true;
      }
      else if (this.Station2 == null)
      {
        Station2 = newSt;
        return true;
      }
      else if (this.Station3 == null)
      {
        Station3 = newSt;
        return true;
      }
      else if ((this.Station4 == null) && (this.can_buy_fourth_station))
      {
        Station4 = newSt;
        return true;
      }
      else return false;
    }
    public void DeleteStation(int num)
    {
      if (Station1.Number == num)
        Station1 = null;
      if (Station2.Number == num)
        Station2 = null;
      if (Station3.Number == num)
        Station3 = null;
      if ((Station4 != null) && (Station4.Number == num))
        Station4 = null;
      m_stations_num--;
    }
  }
}
